package fi.vamk.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class menuList extends Activity implements View.OnClickListener {
	public static int welcomeCount =0;
	ImageButton directory;
	ImageButton news;
	ImageButton map;
	ImageButton classwork;
	ImageButton timetable;
	ImageButton address;
	public static String contact, getNews, getClasswork, getDay;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        welcomeCount++;
        if ( welcomeCount == 1)
        {
        	alertOkOnly("Vaasa"," Welcome "+ menu.name.toString()+ " to our mobile app");
        }
        directory=(ImageButton)findViewById(R.id.directory);
        directory.setOnClickListener(this);
        news=(ImageButton)findViewById(R.id.news);
        news.setOnClickListener(this);
        map=(ImageButton)findViewById(R.id.map);
        map.setOnClickListener(this);
        classwork=(ImageButton)findViewById(R.id.classwork);
        classwork.setOnClickListener(this);
        timetable=(ImageButton)findViewById(R.id.timetable);
        timetable.setOnClickListener(this);
        address=(ImageButton)findViewById(R.id.address);
        address.setOnClickListener(this);
    }
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.layout.menu_button, menu);
	        return true;
	    }
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        // Handle item selection
	        switch (item.getItemId()) {
	        case R.id.list:
	        	startActivity(new Intent(menuList.this, menu.class));
	         	finish();
	         	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	        }
	    }
	public void onClick(View view) {
    	if (view == directory)
    	{
    		try {
    			contact = JSONParser.getJSONFromUrl(menu.IpAddress + "student.php?myid="+menu.userId.toString()).toString();
    			startActivity(new Intent(menuList.this, Contact.class));
	         	finish();
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    	}
    	if (view == news)
    	{
    		try {
    			getNews= JSONParser.getJSONFromUrl(menu.IpAddress + "news.php").toString();
    			startActivity(new Intent(menuList.this, news.class));
	         	finish();
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    	}
    	if (view == map)
    	{
    		try {
    			startActivity(new Intent(menuList.this, Map.class));
	         	finish();
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    	}
    	if (view == classwork)
    	{
    		try {
    			getClasswork= JSONParser.getJSONFromUrl(menu.IpAddress + "classwork.php?myid="+menu.userId.toString()).toString();
    			startActivity(new Intent(menuList.this, Classwork.class));
	         	finish();
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    	}
    	if (view == timetable)
    	{
    		try {
    			getDay= JSONParser.getJSONFromUrl(menu.IpAddress + "day.php?").toString();
    			startActivity(new Intent(menuList.this, timetable.class));
	         	finish();
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    	}
    	if (view == address)
    	{
    		try {
    			setContentView(R.layout.address);
			} catch (Exception e) {
				// TODO: handle exception
				alertOkOnly("Exception!", e.toString());
			}
    	}
	}
	public void alertOkOnly(String title, String message)
    {
    	AlertDialog.Builder builder=
        	new AlertDialog.Builder(menuList.this);
        	builder
        	.setTitle(title)
        	.setMessage(message)
        	.setPositiveButton("OK", null)
        	.show();
    } 
}